package cn.harry.modular.society.service.impl;

import cn.harry.modular.society.vo.EchartsOptionVO;
import cn.harry.modular.system.domain.SysDictData;
import cn.harry.modular.system.domain.SysUser;
import cn.harry.modular.system.service.SysDictDataService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.harry.modular.society.domain.Huiyuanyonghu;
import cn.harry.modular.society.service.HuiyuanyonghuService;
import cn.harry.modular.society.mapper.HuiyuanyonghuMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【t_huiyuanyonghu(会员用户)】的数据库操作Service实现
 * @createDate 2025-04-12 20:50:54
 */
@Service
@AllArgsConstructor
public class HuiyuanyonghuServiceImpl extends ServiceImpl<HuiyuanyonghuMapper, Huiyuanyonghu>
        implements HuiyuanyonghuService {

    private final SysDictDataService sysDictDataService;

    @Override
    public void saveUser(SysUser user) {
        Huiyuanyonghu item = new Huiyuanyonghu();
        item.setYonghuzhanghao(user.getUsername());
        item.setYonghuxingming(user.getUsername());
        item.setXingbie(user.getSex());
        item.setBumen(user.getDeptName());
        item.setZhaopian(user.getIcon());
        item.setYonghushouji(user.getPhone());
        save(item);
    }

    @Override
    public List<Map<String, Object>> getMemberCount() {
        List<Huiyuanyonghu> list = list();
        for (Huiyuanyonghu item : list) {
            if (StrUtil.isBlank(item.getDiqu())) {
                item.setDiqu("未知");
            }
        }
        List<Map<String, Object>> vo = new ArrayList<>();

        Map<String, List<Huiyuanyonghu>> map = list.stream()
                .collect(Collectors.groupingBy(Huiyuanyonghu::getDiqu));

        for (Map.Entry<String, List<Huiyuanyonghu>> entry : map.entrySet()) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", entry.getKey());
            map1.put("value", entry.getValue().size());
            vo.add(map1);
        }
        return vo;

    }

    @Override
    public List<Map<String, Object>> getMemberCountByTypePie() {
        List<Huiyuanyonghu> list = list();
        for (Huiyuanyonghu item : list) {
            if (StrUtil.isBlank(item.getDiqu())) {
                item.setDiqu("未知");
            }
        }
        List<Map<String, Object>> vo = new ArrayList<>();

        Map<String, List<Huiyuanyonghu>> map = list.stream().filter(item -> StrUtil.isNotBlank(item.getType()))
                .collect(Collectors.groupingBy(Huiyuanyonghu::getType));

        for (Map.Entry<String, List<Huiyuanyonghu>> entry : map.entrySet()) {
            Map<String, Object> map1 = new HashMap<>();
            SysDictData type = sysDictDataService.getOne(Wrappers.<SysDictData>lambdaQuery()
                    .eq(SysDictData::getDictType, "huiyuan_type").eq(SysDictData::getDictValue, entry.getKey()));
            map1.put("name", type.getDictLabel());
            map1.put("value", entry.getValue().size());
            vo.add(map1);
        }
        return vo;
    }

    @Override
    public EchartsOptionVO getMemberCountByType() {
        EchartsOptionVO vo = new EchartsOptionVO();
        List<Huiyuanyonghu> list = list();
        for (Huiyuanyonghu item : list) {
            if (StrUtil.isBlank(item.getType())) {
                item.setType("未知");
            }
        }
        List<SysDictData> dictDataList =
                sysDictDataService.lambdaQuery()
                        .eq(SysDictData::getDictType, "huiyuan_type")
                        .eq(SysDictData::getStatus, "1")
                        .orderByAsc(SysDictData::getDictSort)
                        .list();

        if (CollUtil.isNotEmpty(list)) {
            // 根据成果类型分组
            JSONArray x = new JSONArray();
            JSONArray y = new JSONArray();
            for (SysDictData sysDictData : dictDataList) {
                x.put(sysDictData.getDictLabel());
                String type = sysDictData.getDictValue();
                int count = list.stream().filter(item -> type.equals(item.getType())).toList().size();
                y.put(count);
                vo.setXData(x);
                vo.setYData(y);
            }
        }
        return vo;
    }
}




